/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-gateway-wechat
*
* @File name : CryptoTools.java
*
* @Author : Administrator
*
* @Date : 2017年6月28日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年6月28日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package com.yonyou.gmcc.gateway.wechat.util;


/**
*
* @author Administrator
* TODO description
* @date 2017年6月28日
*/

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
 
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
/**
* 使用DES加密和解密的方法
* 
* @修改者  小老虎2
* @author:azhong 
* @change:exmorning
* */
@SuppressWarnings({ "unused", "restriction" })
public class CryptoTools {

   private final byte[] DESkey = "kingfykj".getBytes("UTF-8");// 设置密钥，略去
   private AlgorithmParameterSpec iv = null;// 加密算法的参数接口，IvParameterSpec是它的一个实现
   private Key key = null;

   public CryptoTools() throws Exception {
       DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
       iv = new IvParameterSpec(DESkey);//new IvParameterSpec(DESIV);// 设置向量
       SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
       key = keyFactory.generateSecret(keySpec);// 得到密钥对象
       System.out.println(key);
   }

public String encode(String data) throws Exception {
       Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
       enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式，给出密钥和向量
       byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
       BASE64Encoder base64Encoder = new BASE64Encoder();
       return base64Encoder.encode(pasByte);
   }

public String decode(String data) throws Exception {
       Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
       deCipher.init(Cipher.DECRYPT_MODE, key, iv);
       BASE64Decoder base64Decoder = new BASE64Decoder();

       byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));

       return new String(pasByte, "UTF-8");
   }
//   public static void main(String[] args) {
//       try {
//           String test = "oXrcfuADX-Hal72Ct-TxRJLSMj7o";
//           CryptoTools des = new CryptoTools();//自定义密钥
//           System.out.println("加密前的字符："+test);
//           System.out.println("加密后的字符："+URLEncoder.encode(des.encode(test), "utf-8"));
//           System.out.println("解密后的字符："+des.decode(des.encode(test)));
//       } catch (Exception e) {
//           e.printStackTrace();
//       }
//   }
}